package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.mapper.EnrollCustomerMapper;
import com.xmy.cultivate.mapper.EnrollDataMapper;
import com.xmy.cultivate.mapper.EnrollInfoMapper;
import com.xmy.cultivate.mapper.EnrollTeamMemberMapper;
import com.xmy.cultivate.service.IEnrollTeamMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 招生战队成员 服务实现类
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@Service
public class EnrollTeamMemberServiceImpl extends ServiceImpl<EnrollTeamMemberMapper, EnrollTeamMember> implements IEnrollTeamMemberService {

    @Autowired
    EnrollTeamMemberMapper enrollTeamMemberMapper;

    @Autowired
    EnrollInfoMapper enrollInfoMapper;

    @Autowired
    EnrollCustomerMapper enrollCustomerMapper;

    @Autowired
    EnrollDataMapper enrollDataMapper;


    public Long getTeamIdForteacherId(Long teacherId) {

        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
        enrollTeamMemberQueryWrapper.eq("teacher_id", teacherId);
        enrollTeamMemberQueryWrapper.eq("deleted", 0);
        enrollTeamMemberQueryWrapper.eq("status", 1);
        enrollTeamMemberQueryWrapper.last("limit 1");
        enrollTeamMemberQueryWrapper.orderByDesc("id");

        Long teamId = 0L;

        EnrollTeamMember enrollTeamMember = enrollTeamMemberMapper.selectOne(enrollTeamMemberQueryWrapper);
        if (enrollTeamMember != null) {
            teamId = enrollTeamMember.getTeamId();
        }

        return teamId;
    }

    @Transactional
    public boolean switchTeam(EnrollTeamMember enrollTeamMember) {

        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();

        //System.out.println("enrollTeamMember:"+enrollTeamMember.toString());
        enrollTeamMemberQueryWrapper.eq("teacher_id", enrollTeamMember.getTeacherId());
        enrollTeamMemberQueryWrapper.eq("status", 1);
        enrollTeamMemberQueryWrapper.eq("deleted", 0);
        enrollTeamMemberQueryWrapper.last("limit 1");


        EnrollTeamMember enrollTeamMemberInfo = enrollTeamMemberMapper.selectOne(enrollTeamMemberQueryWrapper);
        //System.out.println("enrollTeamMemberInfo:"+enrollTeamMemberInfo.toString());

        boolean rs = false;

        if (enrollTeamMemberInfo != null) {
            if (enrollTeamMember.getEndDate() != null) {
                QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();
                enrollInfoQueryWrapper.eq("teacher_id", enrollTeamMember.getTeacherId());
                enrollInfoQueryWrapper.eq("deleted", 0);
                enrollInfoQueryWrapper.gt("add_date", enrollTeamMember.getEndDate());
                List<EnrollInfo> enrollInfoList = enrollInfoMapper.selectList(enrollInfoQueryWrapper);
                for (EnrollInfo enrollInfo : enrollInfoList) {
                    enrollInfo.setTeamId(enrollTeamMember.getTeamId());
                    enrollInfoMapper.updateById(enrollInfo);
                }
                //设置原来战队结束
                enrollTeamMemberInfo.setEndDate(enrollTeamMember.getEndDate());
                enrollTeamMemberInfo.setStatus(0);
                enrollTeamMemberMapper.updateById(enrollTeamMemberInfo);
                //添加到新的战队
                enrollTeamMemberQueryWrapper = new QueryWrapper<>();
                enrollTeamMemberQueryWrapper.eq("teacher_id", enrollTeamMember.getTeacherId());
                enrollTeamMemberQueryWrapper.eq("status", 1);
                enrollTeamMemberQueryWrapper.eq("deleted", 0);
                Long count = enrollTeamMemberMapper.selectCount(enrollTeamMemberQueryWrapper);
                if (count.equals(0L)) {
                    EnrollTeamMember enrollTeamMemberAddInfo = new EnrollTeamMember();
                    enrollTeamMemberAddInfo.setTeamId(enrollTeamMember.getTeamId());
                    enrollTeamMemberAddInfo.setTeacherId(enrollTeamMember.getTeacherId());
                    enrollTeamMemberAddInfo.setStartDate(enrollTeamMember.getEndDate().plusDays(1));
                    enrollTeamMemberAddInfo.setStatus(1);
                    enrollTeamMemberMapper.insert(enrollTeamMemberAddInfo);
                }

                QueryWrapper wrapper = new QueryWrapper<>();
                wrapper.gt("add_date", enrollTeamMember.getEndDate());
                wrapper.eq("teacher_id", enrollTeamMember.getTeacherId());
                List<EnrollCustomer> enrollCustomerList = enrollCustomerMapper.selectList(wrapper);
                for (EnrollCustomer customer : enrollCustomerList) {
                    customer.setTeamId(enrollTeamMember.getTeamId());
                    enrollCustomerMapper.updateById(customer);
                }

                List<EnrollData> enrollDataList = enrollDataMapper.selectList(wrapper);
                for (EnrollData enrollData : enrollDataList) {
                    enrollData.setTeamId(enrollTeamMember.getTeamId());
                    enrollDataMapper.updateById(enrollData);
                }

                rs = true;
            }
        }
        return rs;
    }
}
